package com.zt.rongda.salary.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zt.rongda.salary.model.SalaryEmpQuota;
import com.zt.rongda.salary.service.SalaryEmpQuotaService;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/salaryEmpQuota")
public class SalaryEmpQuotaController {

    @Autowired
    private SalaryEmpQuotaService salaryEmpQuotaService;

    @RequestMapping(value = "/query")
    @ResponseBody
    
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(salaryEmpQuotaService.getTable(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        salaryEmpQuotaService.add(getSalaryEmpQuota(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        salaryEmpQuotaService.update(getSalaryEmpQuota(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        salaryEmpQuotaService.delete(reqBean.getInfo("empId"), reqBean.getInfo("workStepId"));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private SalaryEmpQuota getSalaryEmpQuota(ReqBean reqBean) {
        SalaryEmpQuota salaryEmpQuota = new SalaryEmpQuota();
        salaryEmpQuota.setEmpId(reqBean.getInfo("empId"));
        salaryEmpQuota.setQuotaConfigType(reqBean.getInfo("quotaConfigType"));
        salaryEmpQuota.setEmpQuota(reqBean.getInfoDouble("empQuota"));
        salaryEmpQuota.setOperator(reqBean.getUserId());
        salaryEmpQuota.setRemark(reqBean.getInfo("remark"));
        return salaryEmpQuota;
    }

}